//
//  VisitorLoginView.m
//  OC-LBWeibo
//
//  Created by 李博 on 15/6/24.
//  Copyright © 2015年 libo. All rights reserved.
//

#import "VisitorLoginView.h"

@interface VisitorLoginView()

@property(nonatomic,strong)UIImageView *iconView;
@property(nonatomic,strong)UIImageView *maskIconView;
@property(nonatomic,strong)UIImageView *homeIconView;
@property(nonatomic,strong)UILabel *messageLabel;
@property(nonatomic,strong)UIButton *registerButton;
@property(nonatomic,strong)UIButton *loginButton;

@end

@implementation VisitorLoginView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

//设置视图内容
- (void)setupViewInfoWithIconName:(NSString *)iconName message:(NSString *)message isHome:(BOOL)isHome
{
    self.messageLabel.text = message;
    if (isHome) {
        self.homeIconView.hidden = NO;
        self.iconView.image = [UIImage imageNamed:@"visitordiscover_feed_image_smallicon"];
    }else{
        self.homeIconView.hidden = YES;
        self.iconView.image = [UIImage imageNamed:iconName];
        [self sendSubviewToBack:self.maskIconView];
    }
}
//动画
- (void)startAnimation
{
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    anim.toValue = @(2 * M_PI);
    anim.duration = 20;
    anim.repeatCount = MAXFLOAT;
    
    [self.iconView.layer addAnimation:anim forKey:nil];
}

//用户注册
- (void)userRegister {
    
    if ([self.delegate respondsToSelector:@selector(visitorRegisterButtonClicked)]) {
        [self.delegate visitorRegisterButtonClicked];
    }
    
}
//用户登录
- (void)userLogin {
    
    if ([self.delegate respondsToSelector:@selector(visitorLoginButtonClicked)]) {
        [self.delegate visitorLoginButtonClicked];
    }
    
}



- (void)setupUI
{
    [self addSubview:self.iconView];
    [self addSubview:self.maskIconView];
    [self addSubview:self.homeIconView];
    [self addSubview:self.messageLabel];
    [self addSubview:self.registerButton];
    [self addSubview:self.loginButton];
    
    
    //2.自动布局
    //iconView
    self.iconView.translatesAutoresizingMaskIntoConstraints = NO;
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.iconView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.iconView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:-60]];
    
    
    //homeIconView
    self.homeIconView.translatesAutoresizingMaskIntoConstraints = NO;
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.homeIconView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.iconView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.homeIconView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.iconView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
    
    
    self.messageLabel.translatesAutoresizingMaskIntoConstraints = NO;
    //x
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.messageLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.iconView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
    //y
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.messageLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.iconView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:20]];
    //width -vfl
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[subView(240)]" options:NSLayoutFormatAlignAllBaseline metrics:nil views:@{@"subView":self.messageLabel}]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[subView(40)]" options:NSLayoutFormatAlignAllBaseline metrics:nil views:@{@"subView":self.messageLabel}]];

    
    
    self.registerButton.translatesAutoresizingMaskIntoConstraints = NO;
    //x
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.registerButton attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.messageLabel attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
    //y
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.registerButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.messageLabel attribute:NSLayoutAttributeBottom multiplier:1.0 constant:20]];
    //width -vfl
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[subView(100)]" options:NSLayoutFormatAlignAllBaseline metrics:nil views:@{@"subView":self.registerButton}]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[subView(40)]" options:NSLayoutFormatAlignAllBaseline metrics:nil views:@{@"subView":self.registerButton}]];

    self.loginButton.translatesAutoresizingMaskIntoConstraints = NO;
    //x
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.loginButton attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.messageLabel attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]];
    //y
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.loginButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.messageLabel attribute:NSLayoutAttributeBottom multiplier:1.0 constant:20]];
    //width -vfl
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[subView(100)]" options:NSLayoutFormatAlignAllBaseline metrics:nil views:@{@"subView":self.loginButton}]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[subView(40)]" options:NSLayoutFormatAlignAllBaseline metrics:nil views:@{@"subView":self.loginButton}]];

    //遮罩视图布局
    self.maskIconView.translatesAutoresizingMaskIntoConstraints = NO;
    //水平
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[subView]|" options:NSLayoutFormatAlignAllBaseline metrics:nil views:@{@"subView":self.maskIconView}]];
    //垂直
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[subView]-200-|" options:NSLayoutFormatAlignAllBaseline metrics:nil views:@{@"subView":self.maskIconView}]];
    
    self.backgroundColor = [UIColor colorWithWhite:237.0 / 255.0 alpha:1.0];
    
    
}

- (UIImageView *)iconView
{
    if (_iconView == nil) {
        UIImageView *iconView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"visitordiscover_feed_image_smallicon"]];
        _iconView = iconView;
    }
    return _iconView;
}
- (UIImageView *)maskIconView
{
    if (_maskIconView == nil) {
        UIImageView *maskIconView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"visitordiscover_feed_mask_smallicon"]];
        _maskIconView = maskIconView;
    }
    return _maskIconView;
}
- (UIImageView *)homeIconView
{
    if (_homeIconView == nil) {
        UIImageView *homeIconView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"visitordiscover_feed_image_house"]];
        _homeIconView = homeIconView;
    }
    return _homeIconView;
}
- (UILabel *)messageLabel
{
    if (_messageLabel == nil) {
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 240, 44)];
        label.text = @"登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过";
        label.textColor = [UIColor darkGrayColor];
        label.font = [UIFont systemFontOfSize:14];
        label.textAlignment = NSTextAlignmentCenter;
        label.preferredMaxLayoutWidth = 240;
        label.numberOfLines = 2;
        
        [label sizeToFit];
        _messageLabel = label;
    }
    return _messageLabel;
}

- (UIButton *)registerButton
{
    if (_registerButton == nil) {
        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
        [btn setTitle:@"注册" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"common_button_white_disable"] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(userRegister) forControlEvents: UIControlEventTouchUpInside];
        _registerButton = btn;
    }
    return _registerButton;
}

- (UIButton *)loginButton
{
    if (_loginButton == nil) {
        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(220, 220, 100, 40)];
        [btn setTitle:@"登录" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"common_button_white_disable"] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(userLogin) forControlEvents: UIControlEventTouchUpInside];
        _loginButton = btn;
    }
    return _loginButton;
}


@end
